#include "itemwidgetpicture.h"
#include "ui_itemwidgetpicture.h"
#include "personal_space.h"
#include "QMenu"
#include "QDesktopServices"
#include "QUrl"
#include "QNetworkRequest"
#include "QNetworkReply"

static Personal_Space * ptrParent;

ItemWidgetPicture::ItemWidgetPicture(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ItemWidgetPicture)
{
    ui->setupUi(this);
    ptrParent = static_cast<Personal_Space *>(this->parentWidget());
}

//点击播放图片槽函数
void ItemWidgetPicture::mediapicture(QString ppath)
{
    QString path = QString("file:///") + ppath;
    QDesktopServices::openUrl(QUrl(path));
}

void ItemWidgetPicture::dealMsg(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QString s = array;
    if(QString::compare(s,"图片修改成功！") == 0)
    {
        QString preName = ui->lineEdit_pinfo->text();
        emit prename(qstrid, preName);
    }
}

//重命名的槽函数
void ItemWidgetPicture::saveEditinfo(int pictureid)
{
    QString pname;
    pname = ui->lineEdit_pinfo->text();
    // URL
    QString baseUrl = "http://127.0.0.1:5000/PictureUpdate?Pictureid=" + QString::number(pictureid) + "&Picturename=" + pname;
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg(QNetworkReply*)));

    ui->lineEdit_pinfo->setEnabled(false);
}
void ItemWidgetPicture::ReName(int pictureid)
{
    ui->lineEdit_pinfo->setEnabled(true);
    connect(ui->lineEdit_pinfo,&QLineEdit::returnPressed,this,[=](){saveEditinfo(pictureid);});
}


//删除的槽函数
void ItemWidgetPicture::Delete(int pictureid)
{
    // URL
    QString baseUrl = "http://127.0.0.1:5000/PictureDelete?Pictureid=" + QString::number(pictureid);
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    connect(manager, &QNetworkAccessManager::finished, [=](QNetworkReply* reply){
        QByteArray array = reply->readAll();
        QString s = array;
        if(s.compare("图片删除成功") == 0)
        {
            emit remake();
        }
    });
}

void ItemWidgetPicture::SetData(const int &qstrid, const QString& qstrFileName,  const QString& qstrPic)
{
    //保存图片id
    int pictureid = qstrid;

    this->qstrid = qstrid;
    this->qstrFileName = qstrFileName;
    this->qstrPic = qstrPic;

    //文本框显示文件名
    ui->lineEdit_pinfo->setText(qstrFileName);
    ui->lineEdit_pinfo->setEnabled(false);

    //显示视频封面或图片
    QPixmap pixmapPic(qstrPic);
    int iWidth = ui->label_picture->width();
    int iHeight = ui->label_picture->height();
    QPixmap pixmapPicFit = pixmapPic.scaled(iWidth, iHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_picture->setPixmap(pixmapPicFit);

    //设置按钮菜单栏
    QMenu *m = new QMenu(this);
    m->setStyleSheet("QMenu{background-color:rgb(255,255,255)}QMenu::item:selected{background-color:blue}");
    QAction* pActionRename = m->addAction("重命名");
    QAction* pActionDel = m->addAction("删除");

    //消除“未引用的局部变量”警告
    Q_UNUSED(pActionRename)
    //Q_UNUSED(pActionLoad)
    Q_UNUSED(pActionDel)

    //设置按钮
    ui->pushButton_pmenu->setMenu(m);
    ui->pushButton_pmenu->setStyleSheet("QPushButton::menu-indicator{image:None;}");


    //连接点击删除图片槽函数
    connect(pActionDel,&QAction::triggered,this,[=](){Delete(pictureid);});
    //连接点击重命名图片槽函数
    connect(pActionRename,&QAction::triggered,this,[=](){ReName(pictureid);});
    //连接点击播放图片槽函数
    connect(ui->label_picture,&MyClickLabel::clicked,this,[=](){mediapicture(qstrPic);});
}

ItemWidgetPicture::~ItemWidgetPicture()
{
    delete ui;
}
